package com.huatai.bpmn.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.bpmn.entity.*;
import com.huatai.bpmn.entity.vo.YYBasicEntityVo;
import com.huatai.bpmn.mapper.YYExternalParameterMapper;
import com.huatai.bpmn.service.*;
import com.huatai.common.util.BeanUtil;
import com.huatai.common.util.ObjectUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.user.dubbo.UserDubboApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxy
 * @since 2023-08-14
 */
@Service
public class YYExternalParameterServiceImpl extends ServiceImpl<YYExternalParameterMapper,YYExternalParameterEntity> implements YYExternalParameterService {
	@Autowired
	private YYBasicService basicService;
	@Autowired
	private YYFormService yyFormService;
	@Autowired
	private DicService dicService;
	@DubboReference(timeout = 30000, check = false, retries = 0)
	private UserDubboApi userDubboApi;

	/**
	 * 根据id查询表头和外部参数
	 * @param id
	 * @return
	 */
	@Override
	public HashMap<String, Object> selectColumAndParamById(Long id) {
		YYExternalParameterEntity externalParameterEntity = this.getById(id);
		HashMap<String, Object> paramMap = getParam(externalParameterEntity);
		return paramMap	;
	}

	private HashMap<String, Object> getParam(YYExternalParameterEntity externalParameterEntity) {
		if (ObjectUtil.isEmpty(externalParameterEntity)){
			throw new RuntimeException("外部参数id错误");
		}
		String[] placeArr = getStrings(externalParameterEntity.getTableInfo());
		String place = externalParameterEntity.getPlace();

		HashMap<String, Object> paramMap = new HashMap<>();//存储表头和外部参数
		HashMap<String, HashMap<String,Object>> map = new HashMap<>();//存储表头
		Arrays.stream(placeArr).forEach(table->{
			//获取表单表头
			List<YYBasicEntity> colum = basicService.getColum(table);
			ArrayList<YYBasicEntityVo> list = new ArrayList<>();
			for (YYBasicEntity yyBasicEntity : colum) {

				String jsonKey = yyBasicEntity.getJsonKey();
				//归口部门
				if (jsonKey.equals("gkbm")){
					List<JSONObject> childrenList=userDubboApi.getDepartList();
					YYBasicEntityVo yyBasicEntityVo = new YYBasicEntityVo();
					BeanUtil.copy(yyBasicEntity,yyBasicEntityVo);
					yyBasicEntityVo.setChildren(childrenList);
					list.add(yyBasicEntityVo);
					continue;
				}

				YYBasicEntityVo yyBasicEntityVo = new YYBasicEntityVo();
				BeanUtil.copy(yyBasicEntity,yyBasicEntityVo);
				if (!StringUtil.isEmpty(yyBasicEntity.getSelectItem())){
					List<JSONObject> childrenList=dicService.findChildrenData(yyBasicEntity.getSelectItem());
					yyBasicEntityVo.setChildren(childrenList);
				}
				list.add(yyBasicEntityVo);
			}
			YYFormEntity formEntity = yyFormService.getByPlace(table);
			HashMap<String, Object> dataMap = new HashMap<>();
			dataMap.put("colum",list);
			dataMap.put("name",formEntity.getName());
			dataMap.put("allowChange",true);
			map.put(table,dataMap);
		});
		List<YYBasicEntity> colum = basicService.getColum(place);
		ArrayList<YYBasicEntityVo> list = new ArrayList<>();
		for (YYBasicEntity yyBasicEntity : colum) {
			String jsonKey = yyBasicEntity.getJsonKey();
			//归口部门
			if (jsonKey.equals("gkbm")){
				List<JSONObject> childrenList=userDubboApi.getDepartList();
				YYBasicEntityVo yyBasicEntityVo = new YYBasicEntityVo();
				BeanUtil.copy(yyBasicEntity,yyBasicEntityVo);
				yyBasicEntityVo.setChildren(childrenList);
				list.add(yyBasicEntityVo);
				continue;
			}

			YYBasicEntityVo yyBasicEntityVo = new YYBasicEntityVo();
			BeanUtil.copy(yyBasicEntity,yyBasicEntityVo);
			if (!StringUtil.isEmpty(yyBasicEntity.getSelectItem())){
			List<JSONObject> childrenList=dicService.findChildrenData(yyBasicEntity.getSelectItem());
			yyBasicEntityVo.setChildren(childrenList);
			}
			list.add(yyBasicEntityVo);
		}
		paramMap.put("param",list);
		paramMap.put("column",map);
		return paramMap;
	}

	@Override
	public Page<YYExternalParameterEntity> page(Integer pageIndex, Integer pageSize) {
		Page<YYExternalParameterEntity> page = this.page(new Page<>(pageIndex, pageSize));
		//获取记录
		List<YYExternalParameterEntity> records = page.getRecords();
		//针对记录遍历获取place，并查询
		records.forEach(externalParameterEntity->{
			String place = externalParameterEntity.getPlace();
			if (StringUtil.isNotBlank(place)){
				String[] places = place.split(",");
				AtomicReference<String> placeKey = new AtomicReference<>("");
				AtomicReference<String> placeName = new AtomicReference<>("");
				Arrays.asList(places).forEach(x->{
					YYFormEntity formName = yyFormService.getByPlace(x);
					placeKey.updateAndGet(v -> v + formName.getPlace()+",");
					placeName.updateAndGet(name-> name + formName.getName() + ",");
				});
				String placeKeys = placeKey.get().substring(0, placeKey.get().length()-1);
				String placeNames = placeName.get().substring(0,placeKey.get().length()-1);
				externalParameterEntity.setPlace(placeKeys);
//				externalParameterEntity.setPlaceName(placeNames);
			}
		});
		return page;
//		return null;
	}

	@Override
	public Object getDepartList() {
		return userDubboApi.getDepartList();
	}

	private String[] getStrings(String arrStr) {
		if (StringUtil.isEmpty(arrStr)){
			return new String[]{};
		}
		return arrStr.split(",");
	}
}
